/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import utils.DbManager;
import utils.Prodotti;
import utils.Utilizzatore;

/**
 *
 * @author Alice
 */
public class RicercaXNome extends HttpServlet {
  private DbManager manager;
    
 @Override
public void init() throws ServletException {
// inizializza il DBManager dagli attributi di Application
this.manager = (DbManager)super.getServletContext().getAttribute("dbmanager");
}

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    HttpSession session = request.getSession(false);
        if(session.getAttribute("user")==null)
            response.sendRedirect(request.getContextPath() + "/error.jsp");
    
    String username=(String)session.getAttribute("username");
    System.out.println("USENAME="+username);    
    
    String ricerca=request.getParameter("ricerca");
    List<Prodotti> product = null;    
    System.out.println("OK"+ricerca);
    try {
            product=manager.SearchName(ricerca);      
System.out.println("OK2");        
//request.setAttribute("prodotti",product);
//forward(request,response,"/mostra.jsp");
        
        } catch (SQLException ex) {
            System.out.println("OK3");
            Logger.getLogger(RicercaXCategoria.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("OK4"+product.size());
    
        if ( product.isEmpty()){
            String message="Prodotti Non Trovati !";
            Utilizzatore user=(Utilizzatore) session.getAttribute("user");
            String nome=user.getNome(); String cognome=user.getCognome();
            session.setAttribute("prodotti", product);
            response.sendRedirect("utilizzatore/prodotti_utilizzatore.jsp?user="+nome+" "+cognome+"&message="+URLEncoder.encode(message, "UTF-8"));
        }else{
       /** for (int i = 0; i < product.size(); i++) {
    String c = product.get(i).getNome_Prodotto();
    System.out.println("OK5"+c);}*/
        
        session.setAttribute("prodotti", product);
        System.out.println("OK5");

        Utilizzatore user=(Utilizzatore) session.getAttribute("user");
        String nome=user.getNome(); String cognome=user.getCognome();
        response.sendRedirect("utilizzatore/prodotti_utilizzatore.jsp?user="+nome+" "+cognome);
         System.out.println("OK8");
    }}

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
